运行阶段

当我们输入的时候,触发顺序为

  1. textFieldShouldBeginEditing
  2. textFieldDidBeginEditing

当我们按回车的时候

  1. textFieldShouldReturn
  2. textFieldShouldEndEditing
  3. textFieldDidEndEditing

然后我们在textFieldShouldReturn方法中写入代码

[self.myTextField endEditing];

就可以将键盘收起

关闭键盘的两种方式

endEditing vs resignFirstResponder
这两种有什么区别呢?

self.view.endEditing(true) is less efficient because it will cycle through the entire view hierarchy and makes sure anything that can be a firstRepsonder has resigned it.

endEditing会遍历整个视图,确保所有的能够重置为firstResponder的组件进行重置,效率会略低。

If you know which item is currently the responder, it is more efficient and better practice to call resignFirstResponder on it directly.

如果你知道当前的响应者是哪个元素,那么直接重置该元素会是非常高效的。

代码段


#import "ModalViewController.h"

@interface ModalViewController () <UITextFieldDelegate>
- (IBAction)cancelButton:(id)sender;
- (IBAction)save:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *myTextField;

@end

@implementation ModalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myTextField.delegate = self;
    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    NSLog(@"%@",[[NSString alloc] initWithCString:__FUNCTION__ encoding:NSASCIIStringEncoding]);
    return TRUE;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"%@", [[NSString alloc] initWithCString:__FUNCTION__ encoding:NSASCIIStringEncoding]);
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSLog(@"%@", [[NSString alloc] initWithCString:__FUNCTION__ encoding:NSASCIIStringEncoding]);
    return TRUE;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"%@", [[NSString alloc] initWithCString:__FUNCTION__ encoding:NSASCIIStringEncoding]);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog(@"%@",[[NSString alloc] initWithCString:__FUNCTION__ encoding:NSASCIIStringEncoding]);
    [self.myTextField endEditing:TRUE];
    return TRUE;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (IBAction)cancelButton:(id)sender {
    [self dismissViewControllerAnimated:TRUE completion:^{
        NSLog(@"close2");
    }];
}

- (IBAction)save:(id)sender {
    // NSURL *url = [NSURL URLWithString:@"https://baidu.com"];
    // [[UIApplication sharedApplication] openURL:url];
    // [[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://10086"] options:@{} completionHandler:^(BOOL success) {
        NSLog(@"sms success:%@",success ? @"YES": @"NO");
    }];
    
}
@end

jaysun
507 声望25 粉丝

Code is law